<template>
  <!--客户服务-->
  <div>
    <!--查找-->
    <el-row>
      <span style="padding: 0 10px"><i class="font-14 el-icon-s-open"></i></span>
      <el-select v-model="type" placeholder="全部数据" style="width: 120px" @change="changeType">
        <el-option v-for="item in AllOptions" :key="item.value"
                   :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <span style="padding: 0 10px 0 30px"><i class="fn el-icon-message-solid"></i></span>
      <!--<el-input placeholder="请输入内容" style="width: 150px"></el-input>
      <el-button type="text">
        <el-button icon="el-icon-search" size="small" >服务主题</el-button>
      </el-button>-->
      <el-input @change="changeType" placeholder="请输入内容"  suffix-icon="el-icon-search" style="width: 150px" v-model="search"></el-input>
      <span>服务主题</span>
    </el-row>
    <!--客户服务-->
    <el-row style="margin-top: 15px">
      <el-col :span="12">
        <span set-lan="html:客户服务">客户服务</span>
        <small class="text-muted font-13">共{{totalSize}}条</small>
      </el-col>
      <el-col :span="12" class="text-right">
        <el-button type="text" @click="dialogFormVisible = true">
          <el-button size="small" icon="el-icon-circle-plus-outline">新建</el-button>
        </el-button>
      </el-col>
      <!--客户服务dialog-->
      <el-dialog title="客户服务" :visible.sync="dialogFormVisible" @open="openDialog" @close='closeDialog'  width="60%" style="color: #333333"  >
        <el-form :model="customerservice" ref="customerservice" label-width="100px" :rules="rules">
          <el-divider><span class="font-14-bold">客户服务</span></el-divider>
          <el-row>
            <el-col>
              <el-form-item label="主题:">
                <el-input  v-model="customerservice.serviceTheme" style="width: 91%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="对应客户:">
                <el-input :disabled="true" v-model="customerservice.customerByCustomerId.customerName" style="width: 91%"></el-input>
              </el-form-item>
              <div class="form-inline" v-show="isCustomer">
                <el-input v-model="customerValue" @input="findCustomer" clearable size="medium" placeholder="拼音.字母.关键字" style="width: 150px"></el-input>
                <el-button @click="findCustomer" size="medium" icon="el-icon-search"></el-button>
                <el-popover placement="top" width="140" v-model="visible">
                  <ul class="cu-ul" v-if="customerList2 != ''">
                    <li @click="choiceCustomer2(item)" v-for="item in customerList2" >{{item.customerName}}</li>
                  </ul>
                  <div style="color: #5d9cec;margin: 5px 0;text-align: center" v-if="customerList2 == ''"><i class="el-icon-warning-outline"/>没有该客户！</div>
                </el-popover>
                <el-tooltip class="item" effect="dark" content="浏览选择客户" placement="bottom">
                  <el-button @click="innerVisible(cusCurrPage,cusPageSize)" size="medium" icon="el-icon-s-operation"></el-button>
                </el-tooltip>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="服务类型:">
                <el-radio-group v-model="radio1">
                  <el-radio :label=1>答疑</el-radio>
                  <el-radio :label=2>故障排除</el-radio>
                  <el-radio :label=3>培训</el-radio>
                  <el-radio :label=4>升级</el-radio>
                  <el-radio :label=5>其他</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="服务方式:">
                <el-radio-group v-model="radio2">
                  <el-radio :label=6>电话</el-radio>
                  <el-radio :label=7>传真</el-radio>
                  <el-radio :label=8>邮寄</el-radio>
                  <el-radio :label=9>上门</el-radio>
                  <el-radio :label=5>其他</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item  label="开始日期:">
                <el-date-picker v-model="customerservice.serviceStartDate" type="date" style="width: 79%"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="开始时间:">
                <el-time-picker  v-model="customerservice.serviceStartTime" value-format="HH:mm:ss" style="width: 79%"></el-time-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="花费时间:">
                <el-select v-model="customerservice.serviceCostTime" placeholder="未选" style="width: 79%">
                  <el-option v-for="item in times" :key="item.value"
                             :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="联系人:">
                <el-select v-model="customerservice.serviceCustomerName" placeholder="未选"style="width: 79%">
                  <el-option v-for="(item,index) in contactList" :key="index" :label="item.contactName" :value="item.contactName">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="状态:">
                <el-radio-group v-model="radio3">
                  <el-radio :label=10>无需处理</el-radio>
                  <el-radio :label=11>未处理</el-radio>
                  <el-radio :label=12>处理中</el-radio>
                  <el-radio :label=13>处理完成</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="执行人:">
                <el-input :disabled="true" v-model="customerservice.employeeByEmpId.empName" style="width: 91%">
                  <el-button slot="append" icon="el-icon-search"></el-button>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item  label="服务内容:">
                <el-input type="textarea"  v-model="customerservice.serviceContent" :autosize="{ minRows: 2, maxRows: 2}" style="width: 91%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item  label="客户反馈:">
                <el-input type="textarea"  v-model="customerservice.serviceFeedback" :autosize="{ minRows: 2, maxRows: 2}" style="width: 91%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="备注:">
                <el-input  v-model="customerservice.serviceRemarks" type="textarea" :autosize="{ minRows: 2, maxRows: 3}" style="width: 91%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="7">
              <el-form-item>
                <el-button type="primary" @click="addCustomerservice()">保存</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!--内层dialog-->
        <el-dialog width="30%" title="全部客户" :visible.sync="innerDialog" append-to-body>
          <!--选择客户-->
          <el-table :data="customerList" style="width: 100%">
            <el-table-column prop="customerId" label="客户编号" >
            </el-table-column>
            <el-table-column prop="customerName" label="客户名" >
            </el-table-column>
            <el-table-column label="请选择" >
              <template slot-scope="scope">
                <el-button size="mini" @click="choiceCustomer(scope.row)" icon="el-icon-check" type="primary" circle style="margin-left: 10px"></el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row style="margin-top:10px">
            <el-col :span="16">
              <el-pagination layout="prev, pager, next, jumper" :total="cusTotalSize" @current-change="cusChangePage"
                             :current-page.sync="cusCurrPage" :page-size="cusPageSize"></el-pagination>
            </el-col>
          </el-row>
        </el-dialog>
      </el-dialog>
    </el-row>
    <!--客户服务表格-->
    <el-table ref="multipleTable" :data="customerserviceList" tooltip-effect="dark"
              style="width: 100%"  @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" ></el-table-column>
      <el-table-column prop="serviceId" label="ID" wsortable></el-table-column>
      <el-table-column prop="serviceTheme" label="主题" ></el-table-column>
      <el-table-column prop="customerByCustomerId.customerName"  label="对应客户" ></el-table-column>
      <el-table-column prop="serviceType" label="服务类型" >
        <template slot-scope="scope">
          <span v-if="scope.row.serviceType===1">答疑</span>
          <span v-if="scope.row.serviceType===2">故障排除</span>
          <span v-if="scope.row.serviceType===3">培训</span>
          <span v-if="scope.row.serviceType===4">升级</span>
          <span v-if="scope.row.serviceType===5">其他</span>
        </template>
      </el-table-column>
      <el-table-column prop="serviceWay"  label="服务方式" >
        <template slot-scope="scope1">
          <span v-if="scope1.row.serviceWay===6">电话</span>
          <span v-if="scope1.row.serviceWay===7">传真</span>
          <span v-if="scope1.row.serviceWay===8">邮寄</span>
          <span v-if="scope1.row.serviceWay===9">上门</span>
          <span v-if="scope1.row.serviceWay===5">其他</span>
        </template>
      </el-table-column>
      <el-table-column prop="serviceStartDate"  label="开始日期" ></el-table-column>
      <el-table-column prop="employeeByEmpId.empName"  label="执行人" ></el-table-column>
      <el-table-column prop="serviceCostTime"  label="花费时间" >
        <template slot-scope="scope2">
          <span v-if="scope2.row.serviceCostTime===0">1小时</span>
          <span v-if="scope2.row.serviceCostTime===1">2小时</span>
          <span v-if="scope2.row.serviceCostTime===2">半个工作日</span>
          <span v-if="scope2.row.serviceCostTime===3">1个工作日</span>
          <span v-if="scope2.row.serviceCostTime===4">2个工作日</span>
          <span v-if="scope2.row.serviceCostTime===5">2个工作日以上</span>
        </template>
      </el-table-column>
      <el-table-column prop="serviceState"  label="状态" >
        <template slot-scope="scope3">
          <span v-if="scope3.row.serviceState===10">无需处理</span>
          <span v-if="scope3.row.serviceState===11">未处理</span>
          <span v-if="scope3.row.serviceState===12">处理中</span>
          <span v-if="scope3.row.serviceState===13">处理完成</span>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="135px">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="bottom">
            <el-button size="mini" @click="handleEdit(scope.row)"
                       icon="el-icon-edit" type="primary" circle style="margin-left: 10px"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
            <el-button size="mini" @click="handleDelete(scope.row)"
                       icon="el-icon-delete" type="danger" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row style="margin-top:10px">
      <el-col :span="8">
        <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
          <el-button style="padding: 12px 12px" icon="el-icon-delete"
                     @click="handleDeleteMany" type="danger"></el-button>
        </el-tooltip>
      </el-col>
      <el-col :span="16">
        <el-pagination :page-size="pageSize" layout="prev, pager, next, jumper" @current-change="changePage"
                       :total="totalSize"></el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>export default {
  data () {
    return {
      emp: {},
      emp2: {},
      type: '',
      search: '',
      pageSize: 2, // 分页页大小
      currPage: 1, // 当前页码，默认为1
      totalSize: 0, // 总记录数
      radio1: 1,
      radio2: 6,
      radio3: 10,
      customerList: [], // 员工客户名称
      contactList: [], // 员工联系人集合
      cusPageSize: 2, // 分页页大小
      cusCurrPage: 1, // 当前页码，默认为1
      cusTotalSize: 0, // 总记录数
      isCustomer: true, // 客户搜索显示
      innerDialog: false, // 内层dialog客户
      innerDialogEmp: false, // 内层dialog员工
      customerValue: '', // 模糊查询客户
      customerList2: [], // 模糊查询结果
      visible: false, // 模糊搜索结果
      AllOptions: [{
        value: 0,
        label: '全部数据'},
      {
        value: 1,
        label: '答疑'},
      {
        value: 2,
        label: '故障排除'},
      {
        value: 3,
        label: '培训'},
      {
        value: 4,
        label: '升级'},
      {
        value: 5,
        label: '其他'},
      {
        value: 6,
        label: '电话'},
      {
        value: 7,
        label: '传真'},
      {
        value: 8,
        label: '邮寄'},
      {
        value: 9,
        label: '上门'},
      {
        value: 10,
        label: '无需处理'},
      {
        value: 11,
        label: '未处理'},
      {
        value: 12,
        label: '处理中'},
      {
        value: 13,
        label: '处理完成'}],
      dialogFormVisible: false,
      direction: 'rtl',
      customerservice: {
        serviceId: 0,
        serviceTheme: '',
        serviceType: '',
        serviceWay: '',
        serviceStartDate: '',
        serviceStartTime: '',
        serviceCostTime: '',
        serviceState: '',
        serviceContent: '',
        serviceFeedback: '',
        serviceRemarks: '',
        serviceCustomerName: '',
        customerByCustomerId: '',
        employeeByEmpId: ''
      },
      customerserviceList: [],
      times: [ {
        value: 0,
        label: '1小时'
      }, {
        value: 1,
        label: '2小时'
      }, {
        value: 2,
        label: '半个工作日'
      }, {
        value: 3,
        label: '1个工作日'
      }, {
        value: 4,
        label: '2个工作日'
      }, {
        value: 5,
        label: '2个工作日以上'
      }],
      timer: '', // 定义一个定时器的变量
      currentTime: new Date(), // 获取当前时间
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]},
      mulSelection: []
    }
  },
  methods: {
    // 查询当前员工
    loginEmp () {
      this.$axios.get('http://localhost:8088/emp')
        .then(v => {
          this.emp = v.data
          this.emp2 = this.emp
        })
        .catch(function (v) {})
    },
    initData (currPage, pageSize) {
      this.$axios.get('http://localhost:8088/customerservice-list', {params: {page: currPage, size: pageSize, search: this.search, type: this.type}})
        .then(v => {
          this.customerserviceList = v.data.rows
          this.totalSize = v.data.total
        })
        .catch(function (v) {})
    },
    addCustomerservice () {
      this.customerservice.serviceType = this.radio1
      this.customerservice.serviceWay = this.radio2
      this.customerservice.serviceState = this.radio3
      this.$refs.customerservice.validate((valid) => {
        if (valid) {
          this.$axios.post('http://localhost:8088/customerservice-addCustomerservice', this.customerservice)
            .then(v => {
              // 1、刷新数据
              this.initData(this.currPage, this.pageSize)
              // 2、关闭弹框
              this.dialogFormVisible = false
              // 3、重新初始化表单数据模型
              this.customerservice = {
                serviceId: 0,
                serviceTheme: '',
                serviceType: '',
                serviceWay: '',
                serviceStartDate: '',
                serviceStartTime: '',
                serviceCostTime: '',
                serviceState: '',
                serviceContent: '',
                serviceFeedback: '',
                serviceRemarks: '',
                serviceCustomerName: '',
                customerByCustomerId: '',
                employeeByEmpId: ''
              }
            })
            .catch(function (v) {})
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    changePage (val) {
      this.initData(val, this.pageSize, this.search, this.type)
    },
    changeType () {
      this.initData(1, this.pageSize, this.search, this.type)
    },
    handleEdit (val) {
      this.dialogFormVisible = true
      this.customerservice.serviceId = val.serviceId
      this.customerservice.serviceTheme = val.serviceTheme
      this.radio1 = val.serviceType
      this.radio2 = val.serviceWay
      this.radio3 = val.serviceState
      this.customerservice.serviceStartDate = val.serviceStartDate
      this.customerservice.serviceStartTime = val.serviceStartTime
      this.customerservice.serviceCostTime = val.serviceCostTime
      this.customerservice.serviceContent = val.serviceContent
      this.customerservice.serviceFeedback = val.serviceFeedback
      this.customerservice.serviceRemarks = val.serviceRemarks
      this.customerservice.serviceCustomerName = val.serviceCustomerName
      this.customerservice.customerByCustomerId = val.customerByCustomerId
      this.customerservice.employeeByEmpId = val.employeeByEmpId
      this.emp = val.employeeByEmpId
    },
    openView (val) {
      this.$axios.post('http://localhost:8080/#/qaview')
    },
    // 打开dialog事件
    openDialog () {
      this.customerservice.employeeByEmpId = this.emp
    },
    // 关闭dialog事件
    closeDialog () {
      this.$refs.customerservice.resetFields()
      this.customerservice = {
        serviceId: 0,
        serviceTheme: '',
        serviceType: '',
        serviceWay: '',
        serviceStartDate: '',
        serviceStartTime: '',
        serviceCostTime: '',
        serviceState: '',
        serviceContent: '',
        serviceFeedback: '',
        serviceRemarks: '',
        serviceCustomerName: '',
        customerByCustomerId: '',
        employeeByEmpId: ''
      }
      this.isCustomer = true
      this.emp = this.emp2
    },
    // 打开内层dialog
    innerVisible (cusCurrPage, cusPageSize) {
      this.$axios.get('http://localhost:8088/emp-customer-list', {
        params: {
          page: cusCurrPage,
          size: cusPageSize
        }
      })
        .then(v => {
          this.customerList = v.data.rows
          this.cusTotalSize = v.data.total
        })
        .catch(function (v) {})
      this.innerDialog = true
    },
    // 客户分页
    cusChangePage (val) {
      // val就是当前页码
      this.innerVisible(val, this.cusPageSize)
    },
    // 选择客户
    choiceCustomer (val) {
      this.customerservice.customerByCustomerId = val
      console.log(this.customerservice.customerByCustomerId)
      this.choiceContact(val.customerId)
      this.innerDialog = false
    },
    // 模糊查询客户
    findCustomer () {
      // eslint-disable-next-line eqeqeq
      if (this.customerValue != '') {
        this.visible = true
        this.$axios.get('http://localhost:8088/emp-fuzzy-customer-list', {
          params: {
            customerName: this.customerValue
          }
        })
          .then(v => {
            this.customerList2 = v.data
          })
          .catch(function (v) {})
      } else {
        this.visible = false
      }
    },
    // 模糊选择客户
    choiceCustomer2 (val) {
      console.log(val)
      this.customerservice.customerByCustomerId = val
      this.choiceContact(val.customerId)
      this.visible = false
    },
    // 失去焦点关闭模糊搜索框
    closeEmp(){
      this.visible = false
      this.customerValue = ''
    },
    // 选择联系人
    choiceContact (customerId) {
      this.$axios.get('http://localhost:8088/contact-list', {
        params: {
          customerId: customerId
        }
      })
        .then(v => {
          this.contactList = v.data
        })
        .catch(function (v) {})
    },
    // 选择员工
    choiceEmp (val) {
      this.customerservice.employeeByEmpId = val
      console.log(this.customerservice.customerByCustomerId)
      this.$axios.get('http://localhost:8088/contact-list', {
        params: {
          customerId: val.customerId
        }
      })
        .then(v => {
          this.contactList = v.data
        })
        .catch(function (v) {})
      this.innerDialog = false
    },
    handleDelete (val) {
      this.$confirm('此操作将永久删除销售机会, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(val['id'])
        this.$axios.post('http://localhost:8080/save-emp', this.qs.stringify(val['id']))
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleDeleteMany () {
      this.$confirm('此操作将永久删除勾选销售机会, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteAll()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange (val) {
      this.mulSelection = val
    },
    deleteAll () {
      this.mulSelection.forEach(row => {
        console.log(row)
        this.$axios.post('http://localhost:8080/#/qaview', this.qs.stringify(row))
      })
    }
  },
  created: function () {
    this.loginEmp()
    this.initData(this.currPage, this.pageSize, this.search, this.type)
  }
}
</script>

<style>
  .font-14{
    font-size: 14px
  }

  .text-muted {
    color: #98a6ad;
  }
  .font-13 {
    font-size: 13px !important;
  }
  .text-right {
    text-align: right;
    float: right;
  }
  .form-inline{
    position: relative;
    top: -15px;
    left: 100px;
  }
  .font-14-bold{
    color: #000000;
    font-weight: bold;
  }
</style>
